﻿using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Text;
using System;

namespace L2C.Models.Administrator
{
    public class PageContentModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        [AllowHtml]
        public string MainContent { get; set; }
        public string SEOCaption { get; internal set; }

        public static PageContentModel Parse(L2C.StaticContent pageContent)
        {
            return new PageContentModel
            {
                Id = pageContent.ID,
                Title = pageContent.Title,
                MainContent = pageContent.MainContent,
                SEOCaption = pageContent.SEOCaption
            };
        }

        public L2C.StaticContent PopulateToEntity()
        {
            return new StaticContent
            {
                ID = this.Id,
                Title = this.Title,
                MainContent = this.MainContent,
                SEOCaption = Change_AV(this.Title).Replace(' ', '-')
            };
        }

        public string Change_AV(string ip_str_change)
        {
            Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
            char[] BAD_CHARS = new char[] { '!', '@', '#', '$', '%', '_', '"' };
            var result = v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            result = string.Concat(result.Split(BAD_CHARS, StringSplitOptions.RemoveEmptyEntries)).Replace("  ", " ");
            return result;
        } 
    }
}